package com.iamteer.day24;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;

public class UserClient {

	public static void main(String[] args) throws IOException {
		Socket socket = new Socket("192.168.1.240", 9090);
		BufferedReader keyReader = new BufferedReader(new InputStreamReader(System.in, "utf-8"));
//		InputStreamReader socketReader = new InputStreamReader(socket.getInputStream());
		BufferedReader socketReader = new BufferedReader(new InputStreamReader(socket.getInputStream(), "utf-8"));
		OutputStreamWriter socketWriter = new OutputStreamWriter(socket.getOutputStream(), "utf-8");
		while (true) {
			System.out.println("请选择：A 登录	B 注册	Q 退出");
			String option = keyReader.readLine();
			if ("A".equalsIgnoreCase(option)) {
				getInfo(socketWriter, keyReader, option);
				String result = socketReader.readLine();
				System.out.println(result);
			} else if ("B".equalsIgnoreCase(option)) {
				getInfo(socketWriter, keyReader, option);
				String result = socketReader.readLine();
				System.out.println(result);
			} else if ("Q".equalsIgnoreCase(option)) {
				System.out.println("感谢使用，再见！");
				socketWriter.write(option + " quit quit" + "\r\n");
				socketWriter.flush();
				break;
			}
		}
		
		socketWriter.close();
		socketReader.close();
		socket.close();
	}
	
	public static void getInfo(OutputStreamWriter socketWriter, BufferedReader keyReader, String option) {
		String username = "";
		String password = "";
		String info = "";
		try {
			System.out.println("请输入账户");
			username = keyReader.readLine();
			System.out.println("请输入密码");
			password = keyReader.readLine();
			info = option + " " + username + " " + password + "\r\n";
			socketWriter.write(info);
			socketWriter.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
